package ijij41.togetherTrain.common;



import ijij41.togetherTrain.*;
import ijij41.togetherTrain.R.*;
import android.app.*;
import android.content.*;
import android.content.res.*;
import android.os.*;
import android.text.*;
import android.view.*;
import android.widget.*;

public class StationSearchActivity extends Activity implements Constants{

	protected static final int STATION_OK = 1;

	ArrayAdapter<CharSequence> adapter;

	String[] stationItems;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.station_search);

		init();

		findViewById(R.id.btn_back_img).setOnClickListener(mClickListener);
		((EditText) findViewById(R.id.search_text))
				.addTextChangedListener(filterTextWatcher);

		adapter = ArrayAdapter.createFromResource(this, R.array.train_station,
				android.R.layout.simple_list_item_1);

		ListView list = (ListView) findViewById(R.id.list);
		list.setAdapter(adapter);
		list.setOnItemClickListener(mItemClickListener);
		
//		Intent intent = getIntent();
//        String t1 = intent.getStringExtra("TEXT1"); //첫번째 acitvity에서 putExtra하여 설정한 데이타를 얻어온다
//        String t2 = intent.getStringExtra("TEXT2");
      

	}

	private void init() {

		Resources res = getResources();
		stationItems = res.getStringArray(R.array.train_station);

	}

	TextWatcher filterTextWatcher = new TextWatcher() {

		public void afterTextChanged(Editable s) {
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			adapter.getFilter().filter(s);
		}

	};

	Button.OnClickListener mClickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btn_back_img:
				finish();
				break;

			default:
				break;
			}

		}
	};

	AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			setResult(STATION_OK,
					(new Intent()).setAction(stationItems[position]));
			finish();

		}

	};

}
